package android.db.service;

import java.net.HttpURLConnection;
import java.net.URL;

import android.app.gfestival.adapter.DbHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.db.RssItemDAO;
import android.db.dao.AboutDAO;
import android.db.dao.ArtistDAO;
import android.db.dao.ArtistDescriptionDAO;
import android.db.dao.EventDAO;
import android.db.dao.EventDateDAO;

public class DatabaseService {
	SQLiteDatabase db;
	private ArtistDAO artistDAO;
	private ArtistDescriptionDAO artistDescriptionDAO;
	private EventDAO eventDAO;
	private EventDateDAO eventDateDAO;
	private RssItemDAO rssItemDAO;
	private AboutDAO aboutDAO;
	public DatabaseService(Context context){
		DbHelper helper = new DbHelper(context);
		db = helper.getWritableDatabase();
		setArtistDAO(new ArtistDAO(context, db));
		setArtistDescriptionDAO(new ArtistDescriptionDAO(context, db));
		setEventDAO(new EventDAO(context, db));
		setEventDateDAO(new EventDateDAO(context, db));
		setRssItemDAO(new RssItemDAO(context, db));
		setAboutDAO(new AboutDAO(context, db));
	}
	
	
	public boolean isInternetConnected(){
		try {
			String link = "http://www.google.com/";
			URL url = new URL(link );
			HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
			urlc.setConnectTimeout(1000 * 2); // mTimeout is in seconds
			urlc.connect();
			if (urlc.getResponseCode() == 200) {
				return true;
			}
		} catch (Exception e) {
		}
		return false;
	}
	
	public void onDestroy(){
		if(db != null) db.close();
	}



	private void setArtistDAO(ArtistDAO artistDAO) {
		this.artistDAO = artistDAO;
	}



	public ArtistDAO getArtistDAO() {
		return artistDAO;
	}



	private void setArtistDescriptionDAO(ArtistDescriptionDAO artistDescriptionDAO) {
		this.artistDescriptionDAO = artistDescriptionDAO;
	}



	public ArtistDescriptionDAO getArtistDescriptionDAO() {
		return artistDescriptionDAO;
	}



	private void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}



	public EventDAO getEventDAO() {
		return eventDAO;
	}



	private void setEventDateDAO(EventDateDAO eventDateDAO) {
		this.eventDateDAO = eventDateDAO;
	}



	public EventDateDAO getEventDateDAO() {
		return eventDateDAO;
	}


	private void setRssItemDAO(RssItemDAO rssItemDAO) {
		this.rssItemDAO = rssItemDAO;
	}


	public RssItemDAO getRssItemDAO() {
		return rssItemDAO;
	}


	private void setAboutDAO(AboutDAO aboutDAO) {
		this.aboutDAO = aboutDAO;
	}


	public AboutDAO getAboutDAO() {
		return aboutDAO;
	}
}
